package org.jsoft.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoft.dao.SellDAO;

/**
 * Servlet implementation class showMonthKGraph
 */
public class showMonthKGraph extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String year = req.getParameter("year");

		if (null == year) {
			year = new Date().getYear() + 1900 + "";
			System.out.println(year);
		}

		SellDAO dao = new SellDAO();

		TreeMap<String, String> monthMap = dao
				.querySellMoneyGroupByMonthInYear(Integer.parseInt(year));

		TreeSet<String> yearsSet = new TreeSet<String>(dao
				.querySellMoneyGroupByYear().keySet());

		req.setAttribute("yearsSet", yearsSet);

		System.out.println(monthMap);

		req.setAttribute("monthMap", monthMap);
		
		req.setAttribute("year", year);

		req.getRequestDispatcher("/jsp/monthKgraph.jsp").forward(req, resp);
	}

}
